package com.wwwc.index.servlet;

import javax.servlet.*;
import javax.servlet.http.*;
import java.io.*;
import java.util.*;
import java.sql.SQLException;

import javax.ejb.*;
import javax.naming.*;
import java.rmi.*;
import javax.rmi.*;

import com.wwwc.util.web.*;
import com.wwwc.index.ejb.EJBManager;
import com.wwwc.index.ejb.category.*;

public class CategoryServlet extends HttpServlet {
    private Category ejbDb;

    public void doPost(HttpServletRequest request, HttpServletResponse response) {
	
	try {
	    response.setContentType("text/html");

	    String user_ip       = request.getRemoteAddr();
	    HttpSession session  = request.getSession(true);
	    PrintWriter out      = response.getWriter();
	    String call_path     = request.getContextPath()+request.getServletPath();
	   
	    String lid           = (String)session.getAttribute("SelectedLanguageId");
	    if(lid == null) {
		System.out.println("CategoryServlet:Error:lid==null");
		return;
	    }

	    Hashtable mhs = (Hashtable)session.getAttribute("LanguageHash_"+lid);
	    if(mhs == null) {
		return;
	    }

	    String cid = request.getParameter("cid");
	    if(cid == null) {
		cid = (String)session.getAttribute("SelectedCategoryId");
	    }
	    else {
		session.setAttribute("SelectedCategoryId", cid);
	    }

	    if (ejbDb == null) {
		ejbDb = (Category)EJBManager.getEJB("java:comp/env/ejb/CategoryEJB", "Category");
	    }

	    //=========================================================================
	    // Create Category table
	    //=========================================================================

	    if(ejbDb.categoryTableExists() == 0) { //Table not found
		if(ejbDb.createCategoryTable() != 1) {
		    System.out.println("Create category table error.");
		    return;
		}
		else {
		    if(ejbDb.insertCategoryDefaultValue() != 1) {
			ejbDb.dropCategoryTable();
			System.out.println("Insert default category value error.");
			return;
		    }
		}
	    }

	    UserProfile user_profile = (UserProfile)session.getAttribute("UserProfile");

	    int access_level = 0;
	    if(user_profile != null) {
		access_level = user_profile.getUserLevel();
	    }

	    //=========================================================================
	    // List Category 
	    //=========================================================================

	    ArrayList a = (ArrayList)ejbDb.getCategoryList(access_level);
	    CategoryDetails cd = null;
	    Iterator it = a.iterator();

	    out.println("<TABLE width=100% align=center border=0 style='font-size: 10pt'>");
	    out.println("<TR>");
	    
	    int m = 0;
	    while(it.hasNext()) {
		cd = (CategoryDetails)it.next();
		if(cid == null) {
		    if(m == 0) {
			session.setAttribute("SelectedCategory", cd);
			session.setAttribute("SelectedCategoryId", ""+cd.getCategoryId());
		    }
		}

		if(cid != null && cid.equals(""+cd.getCategoryId())) {
		    session.setAttribute("SelectedCategory", cd);
		    session.setAttribute("SelectedCategoryId", ""+cd.getCategoryId());
		}

		out.println("<TD align=center><A href="+call_path+"?lid="+lid+"&cid="+cd.getCategoryId());
		out.println(" style='text-decoration: none;'>");
		out.println(LanguageTrans.getString(session, lid, cd.getCategoryName(), mhs));
		out.println("</A></TD>");
		m++;
	    }
	    out.println("</TR>");
	    out.println("</TABLE>");
	}
	catch (Exception e) {
	    System.out.println("CategoryServlet:Exception:"+e);
	}
    }

    public void doGet(HttpServletRequest request, HttpServletResponse response)
	throws IOException, ServletException 
    {
	doPost(request, response);
    }
}








